<template>
    <div class="app-container">
        <el-row v-if="!convertBuiness">
            <el-col :span="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                    label-width="68px">
                    <el-form-item label="问卷名称" prop="questionnaireName">
                        <el-input v-model="queryParams.questionnaireName" placeholder="请输入问卷名称" clearable
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="发起时间" prop="initiationTime">
                        <el-date-picker clearable v-model="queryParams.initStartTime" type="date"
                            value-format="yyyy-MM-dd" placeholder="请选择开始时间">
                        </el-date-picker>
                        至
                        <el-date-picker clearable v-model="queryParams.initEndTime" type="date"
                            value-format="yyyy-MM-dd" placeholder="请选择结束时间">
                        </el-date-picker>
                    </el-form-item>



                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"
                        :columns="columns"></right-toolbar>
                </el-row>

                <el-table :row-style="{ height: '30px' }" :cell-style="{ padding: '0px' }" height="300" border
                    v-loading="loading" :data="trainingList" highlight-current-row>
                    <el-table-column label="序号" type="index" :index="indexMethod" />
                    <el-table-column label="状态" align="center" prop="status" v-if="columns[0].visible" width="100"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="问卷名称" align="center" prop="questionnaireName" v-if="columns[1].visible"
                        :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="第三方网址" align="center" width="180" prop="thirdPartyUrl"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <template>
                                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                                <el-button @click="handleClick(scope.row.thirdPartyUrl)" type="text">
                                    {{ scope.row.thirdPartyUrl }}
                                </el-button>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column label="发起单位" align="center" prop="initiateUnits" v-if="columns[2].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="发起时间" align="center" prop="initiateTime" width="120"
                        v-if="columns[3].visible" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.initiateTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="联系人" align="center" prop="contacts" v-if="columns[4].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="联系方式" align="center" prop="telephone" v-if="columns[5].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="开始时间" align="center" prop="startTime" width="120" v-if="columns[6].visible">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="结束时间" align="center" prop="endTime" width="120" v-if="columns[7].visible">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="发送数量" align="center" prop="sendNum" v-if="columns[8].visible">
                        <template slot-scope="scope">
                            <!-- Assuming your prop name is regiEnterprise -->
                            <a @click="handleSend(scope.row.questionnaireId)" class="hyperlink">
                                {{ scope.row.sendNum }}
                            </a>
                        </template>
                    </el-table-column>
                    <el-table-column label="回传数量" align="center" prop="returnNum" v-if="columns[9].visible">
                        <template slot-scope="scope">
                            <!-- Assuming your prop name is regiEnterprise -->
                            <a @click="handleReturn(scope.row.questionnaireId)" class="hyperlink">
                                {{ scope.row.returnNum }}
                            </a>
                        </template>
                    </el-table-column>
                    <el-table-column label="附件" align="center" prop="fileUrl" v-if="columns[10].visible">
                        <template slot-scope="scope">
                            <el-button v-if="scope.row.fileUrl != null && scope.row.fileUrl != ''"
                                @click.stop="handlePictureCardPreview(scope.row.fileUpload)" size="small"
                                type="text">附件</el-button>
                            <el-button size="small" v-else type="text">无附件</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建人" align="center" prop="creationName" v-if="columns[11].visible"
                        :show-overflow-tooltip="true" width="100" />
                    <el-table-column label="创建时间" align="center" prop="creationTime" width="120"
                        v-if="columns[12].visible">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="审核人" align="center" prop="examineName" v-if="columns[13].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="40px"
                        fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" @click="handleSelect(scope.row, scope.$index)"
                                icon="el-icon-view" title="查看"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>
        <!-- 添加或修改活动信息培训对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body @close="closeTrain">
            <el-tabs v-model="activeName">
                <el-tab-pane label="基本信息" name="first">
                    <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
                        <div v-if="addSign !== 0">
                            <el-descriptions title="">
                                <el-descriptions-item label="序号">
                                    <el-tag size="medium">{{ form.index }}</el-tag>
                                </el-descriptions-item>
                            </el-descriptions>
                        </div>
                        <el-form-item label="主分类" prop="administrativeArea">
                            <treeselect v-model="form.classificationId" :options="options" placeholder="请选择主分类"
                                v-if="opt != 'details'" style="width: 202.5px" :disabled="ReadOnly" />
                            <el-input v-else v-model="form.label" placeholder="行政区域" />
                        </el-form-item>
                        <el-form-item label="创建人" prop="creationName">
                            <el-input v-model="form.creationName" placeholder="请输入创建人" disabled="true"
                                style="width: 202.5px" />
                        </el-form-item>
                        <el-form-item label="创建时间" prop="creationTime">
                            <el-date-picker clearable v-model="form.creationTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择创建时间" style="width: 202.5px" disabled="true">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="问卷类型" prop="questionnaireType">
                            <el-select v-model="form.questionnaireType" placeholder="请选择形式" style="width: 202.5px"
                                :disabled="isRead">
                                <el-option label="自定义" value="自定义"></el-option>
                                <el-option label="第三方" value="第三方"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="第三方网址" prop="thirdPartyUrl" v-if="form.questionnaireType === '第三方'">
                            <el-input v-model="form.thirdPartyUrl" placeholder="请输入网址" style="width: 202.5px"
                                :disabled="ReadOnly"></el-input>
                        </el-form-item>
                        <el-form-item label="问卷名称" prop="questionnaireName">
                            <el-input v-model="form.questionnaireName" placeholder="请输入问卷名称" :disabled="ReadOnly"
                                style="width: 202.5px" maxlength="50" show-word-limit />
                        </el-form-item>
                        <el-form-item label="发起单位" prop="initiateUnits">
                            <el-input v-model="form.initiateUnits" placeholder="请输入发起单位" :disabled="ReadOnly"
                                style="width: 202.5px" maxlength="30" show-word-limit />
                        </el-form-item>
                        <el-form-item label="发起时间" prop="initiateTime">
                            <el-date-picker clearable v-model="form.initiateTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择发起时间" style="width: 202.5px" :disabled="ReadOnly">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="开始时间" prop="startTime">
                            <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
                                :picker-options="{ disabledDate: dateOptions }" placeholder="请选择开始时间"
                                style="width: 202.5px" :disabled="ReadOnly">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="结束时间" prop="endTime">
                            <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择结束时间" :picker-options="{ disabledDate: dateOptions }"
                                style="width: 202.5px" :disabled="ReadOnly">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="form.contacts" placeholder="请输入联系人" :disabled="ReadOnly"
                                style="width: 202.5px" maxlength="10" show-word-limit />
                        </el-form-item>
                        <el-form-item label="联系电话" prop="telephone">
                            <el-input v-model="form.telephone" placeholder="请输入联系电话" :disabled="ReadOnly"
                                style="width: 202.5px" maxlength="11" show-word-limit />
                        </el-form-item>
                        <el-form-item label="审核人" prop="examineName">
                            <el-input v-model="form.examineName" placeholder="请选择审核人" @focus="selectOpen(0)"
                                :disabled="ReadOnly" style="width: 202.5px" />
                        </el-form-item>
                        <el-form-item label="发送方式" prop="sendType">
                            <el-radio-group v-model="form.sendType" :disabled="ReadOnly">
                                <el-radio label="oneClick">一键发送</el-radio>
                                <el-radio label="groupSend">分类群发</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item v-if="form.sendType === 'groupSend'" label="群发内容" prop="receiverName">
                            <el-input v-model="form.receiverName" type="textarea" placeholder="请输入内容"
                                style="width: 500px" @focus="handleTextAreaFocus" :disabled="ReadOnly" />
                        </el-form-item>
                        <el-form-item label="附件" prop="uploadFile">
                            <file-view v-if="show" :limit="1" :type="typeSelect" :businessId="form.fileUpload"
                                :businessCode="businessCode" style="width: 500px"></file-view>
                        </el-form-item>
                    </el-form>
                    <div class="dialog-footer" v-if="addSign !== 2" style="display: flex; justify-content: center;">
                        <el-button type="primary" @click="submitForm">确 定</el-button>
                        <el-button @click="cancel">关 闭</el-button>
                    </div>
                    <!--          题目-->
                </el-tab-pane>
                <el-tab-pane label="题目" name="second" v-if="addSign !== 0 && form.questionnaireType === '自定义'">
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddTitle"
                                v-if="addSign !== 2">新增</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="exportTitle"
                                v-if="addSign !== 2">导入</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="info" plain size="mini" @click="selectQuestion"
                                v-if="addSign !== 2">选择问卷调查</el-button>
                        </el-col>
                    </el-row>
                    <el-table :data="getRecordList">
                        <el-table-column label="序号" type="index" :index="indexMethodRecord" />
                        <el-table-column label="题目" align="center" prop="topicName" />
                        <el-table-column label="题型" align="center" prop="questionType" />
                        <el-table-column label="选项" align="center" prop="questionnaireAnswersItem" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"
                            v-if="addSign !== 2">
                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-edit"
                                    @click="handleUpdateTopic(scope.row, scope.$index)" title="编辑"></el-button>
                                <el-button size="mini" type="text" icon="el-icon-delete"
                                    @click="handleDeleteTopic(scope.row, scope.$index)" title="删除"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination v-show="totalRecord > 0" :total="totalRecord" :page.sync="queryParamsRecord.pageNum"
                        :limit.sync="queryParamsRecord.pageSize"
                        @pagination="getTitleList(queryParamsRecord.questionnaireId)" />
                </el-tab-pane>
            </el-tabs>
            <!--导入题目-->
            <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
                <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                    :action="upload.url + '?id=' + this.questionnaireId" :disabled="upload.isUploading"
                    :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip text-center" slot="tip">
                        <span>仅允许导入xls、xlsx格式文件。</span>
                        <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                            @click="importTemplate">下载模板</el-link>
                    </div>
                </el-upload>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitFileForm">确 定</el-button>
                    <el-button @click="closeExport">关 闭</el-button>
                </div>
            </el-dialog>
            <!--    导入题目结束-->
        </el-dialog>
        <!--    附件展示对话框-->
        <el-dialog title="查看附件" :visible.sync="openFileList" width="500px" append-to-body v-if="openFileList">
            <UploadFile :upload-id="uploadId" :business-code="businessCode"></UploadFile>
        </el-dialog>
    </div>
</template>

<script>
import { listInvestigationOrgId, getInvestigation, sendList, addInvestigation, updateInvestigation } from "@/api/questionnaire/investigation";
import { listType } from "@/api/maintype/type";
import FileView from "@/components/FileView/index"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listTitle, getTitle, delTitle, addTitle, updateTitle } from "@/api/questionnaire/title";
import Investigation from "./index.vue";
import { getToken } from "../../utils/auth";
import UploadFile from "../upload/index.vue";
export default {
    name: "Training",
    computed: {
        Investigation() {
            return Investigation
        }
    },
    components: { UploadFile, Treeselect, FileView },
    props: {
        receiverId: {
            type: Number,
            default: 0
        },
        initiationId: {
            type: Number,
            default: 0
        }

    },
    data() {
        return {
            uuid: "",
            edit: 'add',
            isClick: true,
            //回传对话框标志位
            openReturn: false,
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            totalRecord: 0,
            typeCode: 'investigation',
            // 活动信息培训表格数据
            trainingList: [],
            getRecordList: [],
            //附件对话框
            openFileList: false,
            fileList: [],
            uploadId: null,
            // 弹出层标题
            title: "",
            InvestigationTitle: "",
            dialogImageUrl: null,
            isRead: false,//控制问卷类型
            ImgDig: false,
            //详情状态
            ReadOnly: false,
            addTitle: '',
            // 是否显示弹出层
            open: false,
            //判断对话框是否新增或编辑
            addSign: 0,
            //新增/编辑题目对话框控制
            openTitle: false,
            //调用新增组件传递的题目id主键
            topicId: null,
            questionnaireId: null,//调用新增题目组件传递的问卷id主键
            openHis: false,//历史问卷调查控制
            //图片的标志位
            show: false,
            typeSelect: 0,
            businessCode: 'investigation',
            //主分类
            options: null,
            userType: "1",
            sign: '2',
            menuId: '2087',
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 15,
                receiverId: null,
                belongTo: null,
                questionnaireName: null,
                initStartTime: null,
                initEndTime: null,
                classificationId: null
            },
            queryParamsRecord: {
                pageNum: 1,
                pageSize: 15,
                questionnaireId: null,
            },
            convertBuiness: false,
            openUser: false,
            //选显卡默认选中
            activeName: 'first',
            // 列信息
            columns: [
                { key: 0, label: `状态`, prop: "status", visible: true },
                { key: 1, label: `问卷名称`, prop: "questionnaireName", visible: true },
                { key: 2, label: `发起单位`, prop: "initiateUnits", visible: true },
                { key: 3, label: `发起时间`, prop: "initiateTime", visible: true },
                { key: 4, label: `联系人`, prop: "contacts", visible: true },
                { key: 5, label: `联系电话`, prop: "telephone", visible: true },
                { key: 6, label: `开始时间`, prop: "startTime", visible: true },
                { key: 7, label: `结束时间`, prop: "endTime", visible: true },
                { key: 8, label: `发送数量`, prop: "sendNum", visible: true },
                { key: 9, label: `回传数量`, prop: "returnNum", visible: true },
                { key: 10, label: `附件`, prop: "fileUrl", visible: true },
                { key: 11, label: `创建人`, prop: "creationName", visible: true },
                { key: 12, label: `创建时间`, prop: "creationTime", visible: true, },
                { key: 13, label: `审核人`, prop: "examineName", visible: true },
            ],
            // 题目导入参数
            upload: {
                // 是否显示弹出层
                open: false,
                // 弹出层标题
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的题目数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/questionnaire/title/importData/"
            },
            //发送企业对话框标志位
            openSend: false,
            //发送企业查询条件
            sendParams: {
                pageNum: 1,
                pageSize: 15,
                questionnaireId: null
            },
            //发送企业列表
            sendEnterpriseList: [],
            //发送企业总数
            sendTotal: 0,
            // 表单参数
            form: {
            },
            // 表单校验
            rules: {

            }
        };
    },
    created() {
        this.getList();
        this.listType();
    },
    methods: {
        /** 查询活动信息培训列表 */
        getList() {
            this.loading = true;
            this.queryParams.receiverId = this.initiationId;
            this.queryParams.belongTo = this.receiverId;
            listInvestigationOrgId(this.queryParams).then(response => {
                this.trainingList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        //报名截止日期控件
        dateOptions(time) {
            return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
        },
        //查询活动对应的题目列表
        getTitleList(questionnaireId) {
            this.queryParamsRecord.questionnaireId = questionnaireId;
            console.log(this.queryParamsRecord)
            console.log(this.questionnaireId)
            listTitle(this.queryParamsRecord).then(res => {
                this.getRecordList = res.rows;
                this.getRecordList.map(function (record, index) {
                    record.questionnaireAnswersItem = record.questionnaireAnswersList.map(function (item, index) {
                        return (index + 1) + '.' + item.solutionName + ' ' + '(' + item.score + ')';
                    }).join(" ");
                }).join("\n");

                this.totalRecord = res.total;
            })
        },
        //题目导入
        exportTitle() {
            this.upload.title = "题目导入";
            this.upload.open = true;
        },
        //查询问卷调查
        selectQuestion() {
            this.openHis = true;
            console.log(this.questionnaireId)
        },
        //获取树节点数据
        listType() {
            listType({ typeCode: 'investigation' }).then(res => {
                console.log(res);
                this.options = res.data;
            })
        },
        selectUser(userId, userName) {
            this.$set(this.form, 'examineId', userId)
            this.$set(this.form, 'examineName', userName)
        },
        //关闭对话框
        closeTrain() {
            //清楚题目id和问卷id
            this.questionnaireId = null;
            this.topicId = null;
            this.isRead = false;
            this.ReadOnly = false;
            this.show = false;
        },
        // 取消按钮
        cancel() {
            //清楚题目id和问卷id
            this.questionnaireId = null;
            this.topicId = null;
            this.isRead = false;
            this.open = false;
            this.show = false;
            this.reset();
        },
        guid() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        },
        //转商机
        convertBusiness(row) {
            this.uuid = this.guid();
            this.edit = "add";
            this.isClick = true;
            this.convertBuiness = true;
        },
        closeEle() {
            this.convertBuiness = false;
        },
        //点击接收方文本域,弹出选择当前用户在企业管理中的企业信息
        handleTextAreaFocus() {
            this.openUser = true;
        },
        selectOpen(type) {
            this.userType = 1;
            this.$refs.select.show();
        },
        //获取当前用户在企业管理中的企业信息
        selectEnterprise(selection) {
            this.form.enterpriseId = selection.map(item => item.enterpriseId);
            this.form.receiverName = selection.map(item => item.enterpriseName).join(',');
            console.log(this.form)
            this.close()
        },
        close() {
            this.openUser = false
        },
        // 表单重置
        reset() {
            this.form = {
                status: null,
                questionnaireId: null,
                questionnaireName: null,
                initiateUnits: null,
                initiateTime: null,
                validityTime: null,
                receiverId: null,
                receiverName: null,
                operateTime: null,
                operateId: null,
                operateName: null,
                departmentId: null,
                departmentName: null,
                organizationId: null,
                organizationName: null,
                creationId: null,
                creationName: null,
                creationTime: null,
                belongTo: null,
                classificationId: null,
                questionnaireType: null,
                contacts: null,
                telephone: null,
                startTime: null,
                endTime: null,
                examineId: null,
                examineName: null,
                fileUpload: null,
                //客户id数组
                enterpriseId: [],
                sendType: null,
                thirdPartyUrl: null,
                questionnairePicture: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },

        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.initStartTime = null;
            this.queryParams.initEndTime = null;
            this.handleQuery();
        },
        //树点击事件
        getNode(data, node, obj) {
            console.log(data)
            if (data.parentId != '00000000-0000-0000-0000-000000000000') {
                this.queryParams.classificationId = data.id
            } else {
                this.queryParams.classificationId = null
            }
            this.getList()
        },
        //当有值的时候，默认打开对话框为分类群发，没值为一键发送
        sendTrain() {
            if (this.form.receiverName !== null) {
                this.sendType = "groupSend"
            } else {
                this.sendType = "oneClick"
            }
        },
        //序号展示
        indexMethod(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        indexMethodRecord(index) {
            return (this.queryParamsRecord.pageNum - 1) * this.queryParamsRecord.pageSize + index + 1;
        },
        //发送企业列表序号
        indexMethodSend(index) {
            return (this.sendParams.pageNum - 1) * this.sendParams.pageSize + index + 1;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.listType();
            this.sendTrain();
            this.form.fileUpload = new Date().getTime();
            this.form.questionnairePicture = new Date().getTime() + 20;
            this.form.creationTime = new Date();
            this.form.questionnaireType = '自定义'
            this.form.creationName = this.$store.state.user.name;
            if (this.queryParams.classificationId === null) {
                this.form.classificationId = '650c44e1-b575-4b62-9d54-1ba9d1e8abb7'
            } else {
                this.form.classificationId = this.queryParams.classificationId;
            }
            this.addSign = 0;
            this.typeSelect = 0;
            this.show = true;
            this.activeName = "first";
            this.title = "添加问卷调查";
        },
        /* 新增题目*/
        handleAddTitle() {
            this.InvestigationTitle = "新增题目";
            this.openTitle = true;
        },
        handleUpdateTopic(row, index) {
            this.questionnaireId = row.questionnaireId;
            this.topicId = row.topicId;
            this.InvestigationTitle = "编辑题目";
            this.openTitle = true;
            this.$refs.saveTitle.topicId = row.topicId;
            this.$refs.saveTitle.getList();
            this.$refs.saveTitle.reset();
            console.log(this.questionnaireId)
            console.log(this.topicId)
        },
        handleDeleteTopic(row, index) {
            this.topicId = row.topicId;
            delTitle(this.topicId).then(res => {
                this.$message.success("删除成功");
                this.getTitleList(row.questionnaireId);
                this.topicId = null;
            })
        },
        /* 关闭题目新增对话框 */
        closeTitle(questionnaireId) {
            this.openTitle = false;
            console.log("返回的题目id" + questionnaireId);
            this.getTitleList(questionnaireId);
            this.$refs.saveTitle.reset();
        },
        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.reset();
            this.questionnaireId = row.questionnaireId || this.ids
            getInvestigation(this.questionnaireId).then(response => {
                this.form = response.data;
                if (this.form.questionnairePicture === null) {
                    this.form.questionnairePicture = new Date().getTime();
                }
                this.sendTrain();
                this.open = true;
                this.typeSelect = 1;
                this.addSign = 1;
                this.form.index = index + 1;
                if (row.status !== '草稿') {
                    this.isRead = true;
                }
                //把接收方的id转成数组
                this.form.enterpriseId = this.form.receiverId.split(',');
                this.title = "修改问卷调查";
                this.getTitleList(row.questionnaireId);
                this.show = true;
                this.activeName = "first";
            });
        },
        /** 查看按钮操作 */
        handleSelect(row, index) {
            this.reset();
            this.questionnaireId = row.questionnaireId || this.ids
            getInvestigation(this.questionnaireId).then(response => {
                this.form = response.data;
                this.ReadOnly = true;
                this.open = true;
                this.isRead = true;
                this.typeSelect = 2;
                this.form.index = index + 1;
                this.addSign = 2;
                this.title = "查看问卷调查";
                this.activeName = "first";
                this.getTitleList(row.questionnaireId);
                this.show = true;
                this.activeName = "first";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.questionnaireId != null) {
                        console.log(this.getRecordList)
                        if (this.getRecordList.length !== 0 && this.form.questionnaireType === '自定义') {
                            this.$message.success("正在发送问卷...");
                            this.$confirm("添加问卷信息后无法修改问卷类型，确定添加？", "提示", {
                                confirmButtonText: "确认",
                                cancelButtonText: "取消",
                                type: "warning",
                            }).then(() => {
                                updateInvestigation(this.form).then(response => {
                                    this.$modal.msgSuccess("修改成功");
                                    this.show = false;
                                    this.isRead = false;
                                    this.open = false;
                                    this.getList();
                                });
                            })
                        } else if (this.getRecordList.length === 0 && this.form.questionnaireType === '自定义') {
                            updateInvestigation(this.form).then(response => {
                                this.$message.warning("问卷类型为自定义，但未输入题目");
                                this.addSign = 1;
                                // this.activeName = "second";
                                this.show = false;
                                this.isRead = false;
                                this.open = false;
                                // this.getTitleList(this.form.questionnaireId);
                                this.getList();
                            });
                        } else if (this.form.questionnaireType === '第三方') {
                            this.$message.success("正在发送问卷...");
                            this.$confirm("添加问卷信息后无法修改问卷类型，确定添加？", "提示", {
                                confirmButtonText: "确认",
                                cancelButtonText: "取消",
                                type: "warning",
                            }).then(() => {
                                updateInvestigation(this.form).then(response => {
                                    this.$modal.msgSuccess("修改成功");
                                    this.show = false;
                                    this.isRead = false;
                                    this.open = false;
                                    this.getList();
                                });
                            })
                        }
                    } else {
                        if (this.form.questionnaireType === '第三方') {
                            this.$message.success("正在发送问卷...");
                            addInvestigation(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.show = false;
                                this.open = false;
                                this.isRead = false;
                                this.getList();
                            });
                        } else {
                            addInvestigation(this.form).then(response => {
                                this.$message.warning("问卷类型为自定义，但未输入题目");
                                this.form.questionnaireId = response.msg;
                                this.addSign = 1;
                                // this.activeName = "second";
                                this.show = false;
                                this.isRead = false;
                                this.open = false;
                                // this.getTitleList(this.form.questionnaireId);
                                this.getList();
                            });
                        }
                        // this.$confirm("添加问卷信息后无法修改类型，确定添加？", "提示", {
                        //   confirmButtonText: "确认",
                        //   cancelButtonText: "取消",
                        //   type: "warning",
                        // })
                        //   .then(() => {
                        //     this.$message.success("正在发送问卷...");
                        //     addInvestigation(this.form).then(response => {
                        //       this.$modal.msgSuccess("新增成功");
                        //       if (this.form.questionnaireType==='第三方'){
                        //         this.show=false;
                        //         this.open=false;
                        //         this.isRead=false;
                        //         this.getList();
                        //       }else {
                        //         this.form.questionnaireId=response.msg;
                        //         this.addSign=1;
                        //         this.activeName = "second";
                        //         this.getTitleList(this.form.questionnaireId);
                        //         this.getList();
                        //       }
                        //     });
                        //   })
                    }
                }
            });
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
            this.getTitleList(this.questionnaireId);
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('/questionnaire/title/importTemplate', {
            }, `题目导入模板.xlsx`)
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
            this.getTitleList(this.questionnaireId);
        },
        closeExport() {
            this.upload.open = false;
        },
        closeHis() {
            this.openHis = false;
            this.getTitleList(this.questionnaireId);
        },
        handleClick(url) {
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
                // 如果没有协议，添加默认的http://协议
                url = 'http://' + url;
            } window.open(url, '_blank');
        },
        //查看发送企业
        handleSend(questionnaireId) {
            this.sendParams.questionnaireId = questionnaireId;
            console.log(this.sendParams.questionnaireId)
            sendList(this.sendParams).then(res => {
                this.sendEnterpriseList = res.rows;
                this.sendTotal = res.total;
                this.openSend = true;
            })
        },
        handlePictureCardPreview(id) {
            this.uploadId = id;
            this.openFileList = true;
        },
        //查看回传企业人员信息
        handleReturn(questionnaireId) {
            this.questionnaireId = questionnaireId;
            this.openReturn = true;
        },
    }
};
</script>
<style scoped>
.hyperlink {
    color: #007BFF;
    /* 蓝色 */
    text-decoration: underline;
    cursor: pointer;
}
</style>
